package com.idriver.controller;

import com.idriver.bean.Student;
import com.idriver.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

@WebServlet(name = "EditStudentServlet", urlPatterns = "/editStudent")
public class EditStudentServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String mail = request.getParameter("mail");
        String mark = request.getParameter("mark");

        Student stu = new Student();
        stu.setId(Integer.parseInt(id));
        stu.setName(name);
        stu.setSex(sex);
        stu.setBirthday(Date.valueOf(birthday));
        stu.setMail(mail);
        stu.setMark(mark);

        StudentService service = new StudentService();
        int i = service.editStudent(stu);
        if (i == 1) {
            response.sendRedirect("");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
